var app = getApp();
Page({
  data: {
    userData: [], // 用户信息
    isShow: false, // 用户是否授权
    winHeight: "", // 窗口高度
    tabbarList: [{
      id: '',
      name: "全部"
    }, {
      id: 0,
      name: "预展 "
    }, {
      id: 1,
      name: "进行中"
    }, {
      id: 2,
      name: "已存档 "
    }, {
      id: -1,
      name: "已结束 "
    }], // 分类列表
    currentTab: 0, // 预设当前项的值
    currentTabId: 0, // 当前tab ID     
    scrollLeft: 0, // tab标题的滚动条位置
    goodsList: [], // 商品数据列表

    noData: 0, // 没有数据
    searchtop: '',
    mask: false,
    overly: false,
    thisid: ''

  },
  soso(e) {
    this.setData({
      searchtop: e.detail.value
      
    })
    console.log(e.detail.value)
  },
  cancelfouce: function(e) {
    var id = e.target.id;
    this.setData({
      mask: true,
      overly: true,
      thisid: id
    })

  },
  // 关注
  fouce:function(e){
    var id = e.target.id;
    this.setData({
      thisid: id
    })
    var that = this;
    wx.getStorage({
      key: 'user',
      success: function (res) {
        wx.request({
          url: app.url.requestUrl + '/focus/auction',
          method: 'POST',
          data: {
            openid: res.data.openid,
            token: res.data.token,
            id: parseInt(that.data.thisid),
          },
          success: function (res) {
            if (res.data.code == 1) {
              wx.showToast({
                title: res.data.msg,
                icon: '',
                duration: 2000,
                mask: true
              })
              that.onLoad()
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: '../img/icon-prompt.png',
                duration: 2000,
                mask: true
              })
            }

          }
        })
      }
    })
  },
  // 取消关注
  cancelfouse: function(e) {
    var that = this;
    wx.getStorage({
      key: 'user',
      success: function(res) {
        wx.request({
          url: app.url.requestUrl + '/focus/goodscancel',
          method: 'POST',
          data: {
            openid: res.data.openid,
            token: res.data.token,
            id: parseInt(that.data.thisid),
          },
          success: function(res) {
            if (res.data.code == 1) {
              that.setData({
                mask: false,
                overly: false,
              })
              wx.showToast({
                title: res.data.msg,
                icon: '',
                duration: 2000,
                mask: true
              })
              that.onLoad()
            }else{
              that.setData({
                mask: false,
                overly: false,
              })
              wx.showToast({
                title: res.data.msg,
                icon: '../img/icon-prompt.png',
                duration: 2000,
                mask: true
              })
            }

          }
        })
      }
    })

  },
  showDialog() {
    this.dialog.showDialog();
  },
  // 滚动切换标签样式
  switchTab: function(e) {
    this.setData({
      currentTab: e.detail.current,
      goodsList: [],
    });
    this.checkCor();
    this.getItemGoodsList(this.data.tabbarList[e.detail.current].id)
  },
  // 点击标题切换当前页时改变样式
  swichNav: function(e) {
    var current = e.target.dataset.current;
    var id = e.target.dataset.id;

    if (this.data.currentTab == current) {
      return false;
    } else {
      this.setData({
        currentTab: current
      })
    }
  },
  getItemGoodsList(id) {
    var self = this;
    var userData = this.data.userData;
    var noData = this.data.noData;
    var datas = this.data.goodsList;
    wx.getStorage({
      key: 'user',
      success: function(res) {
        wx.request({
          url: app.url.requestUrl + '/auction/my_auction',
          method: 'POST',
          data: {
            openid: res.data.openid,
            token: res.data.token,
            type: id,
            search: self.data.searchtop
          },
          success: function(data) {
            var list = data.data;
            if (list.data || datas.length) {
              noData = 0;
            } else {
              noData = 1;
            }
            self.setData({
              noData: noData
            })
            if (data.data.code == 1) {
              self.setData({
                goodsList: list.data.data
              })
            }
          }
        })
      }
    })
  },
  // 搜索
  submit:function(e){
    var self = this;
    wx.getStorage({
      key: 'user',
      success: function (res) {
        wx.request({
          url: app.url.requestUrl + '/auction/my_auction',
          method: 'POST',
          data: {
            openid: res.data.openid,
            token: res.data.token,
            search: self.data.searchtop
          },
          success: function (res) {
            if(res.data.code==1){
              self.setData({
                goodsList: res.data.data.data,
                noData: 0,
              })
            }else{
              console.log(0)
              self.setData({
                noData:1,
                goodsList:[]
              })
            }
           
          }
        })
      }
    })
  },
  
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function() {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  bindGetUserInfo() {
    this.login.hideModalBox()
  },
  onLoad: function() {
    this.getItemGoodsList()
    var self = this;

    this.dialog = this.selectComponent("#dialog");

    this.login = this.selectComponent("#login");

  },
  // 弹窗点击 取消 按钮触发函数
  __cancelEvent() {
    this.dialog.hideDialog();
  },
  // 弹窗点击 确认 按钮触发函数
  __confirmEvent() {
    this.dialog.hideDialog();
  },
  footerTap: app.footerTap
})